$.prototype.serializeObject = function() {
	var a, o, h, i, e;
	a = this.serializeArray();
	o = {};
	h = o.hasOwnProperty;
	for (i = 0; i < a.length; i++) {
		e = a[i];
		if (!h.call(o, e.name)) {
			o[e.name] = e.value;
		}
	}
	return o;
};

$(function () {
	
	//初始化表单
	var codeTemps = new Array();
	var str1 = "demo1";
	var rs = ["A","B","C","D","E","F","G","H"];
	var cs = ["1","2","3","4","5","6","7","8","9","0"];
	var row1 = "<thead><tr><th class=\"pwdtd\"></th>";
	for (var j = 0; j <cs.length ; j++) {
		row1 += "<th class=\"pwdtd\">"+ cs[j] +  "</th>";
	}
	row1 += "</tr></thead>";
	//console.log("row1",row1);
	str1 += row1 + "<tbody>";
	for (var i = 0; i <rs.length ; i++) {
		if(i%2==0){
			str1 += "<tr class=\"pure-table-odd\">";
		}else{
			str1 += "<tr>";
		}
		
		str1 += "<td>" + rs[i] + "</td>";
		for (var j = 0; j <cs.length ; j++) {
			str1 += "<td><input class=\"pwdin\" name=\"" +rs[i] + cs[j] + "\" /></td>";
		}
		str1 += "</tr>";
	}
	str1 += "</tbody>";

	$("#codeTable").html(str1);
	
	if(window.localStorage["userform"]){
		var json = JSON.parse(window.localStorage["userform"]);
		for(var item in json){
			//alert("json中"+item+"的值="+json[item]);
			$("input[name='" + item + "']").val(json[item]);
		}
	}
	if(window.localStorage["pwdform"]){
		var json = JSON.parse(window.localStorage["pwdform"]);
		for(var item in json){
			//alert("json中"+item+"的值="+json[item]);
			$("input[name='" + item + "']").val(json[item]);
		}
	}
	$("#submitSet").click(function(){
		var params = $('#userform').serializeObject();
		//alert(JSON.stringify(params))
		window.localStorage["userform"] = JSON.stringify(params);
		var pwdparams = $('#pwdform').serializeObject();
		//alert(JSON.stringify(pwdparams))
		window.localStorage["pwdform"] = JSON.stringify(pwdparams);
		if(window.localStorage.length>0){
			
		}
	});
	
	$("#submitSet1").click(function(){
		var params = $('#csvcontent').val();
		var rows = params.split("\n");
		var codeObj = {};
		for (var i = 0; i <rows.length ; i++) {
			var row_cols = rows[i].split("	");
			if(row_cols.length==10){
				
				
			}
			
			if(i<rs.length){
				for (var j = 0; j <cs.length ; j++) {
					codeObj[rs[i] + cs[j]] = row_cols[j]
				}
			}
		}
		//Object.keys(myObject).length;
		//console.log(codeObj);
		for(var item in codeObj){
			//alert("json中"+item+"的值="+json[item]);
			$("input[name='" + item + "']").val(codeObj[item]);
		}
	});
});
